package com.leetcode;

import java.util.Stack;

/**
 * @program: leetcode
 * @author: jzhou
 * @date: 2022-11-18 09:47
 * @version: 1.0
 * @description: 用栈实现队列
 * 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作（push、pop、peek、empty）
 **/
public class MyQueue {

    Stack<Integer> inStack ;
    Stack<Integer> outStack ;

    public MyQueue() {
        inStack = new Stack<>();
        outStack = new Stack<>();
    }

    public void push(int x) {
        inStack.push(x);
    }

    public int pop() {
        if(outStack.isEmpty()){
            in2out();
        }
        return  outStack.pop();
    }

    private void in2out() {
        while (!inStack.empty()){
            outStack.push(inStack.pop());
        }
    }

    public int peek() {
        if(outStack.isEmpty()){
            in2out();
        }
        return  outStack.peek();
    }

    public boolean empty() {
        return inStack.isEmpty() && outStack.isEmpty();
    }
}
